#!/usr/bin/env tclsh

puts "hello,tcl!"

# ---------------- demo1 --------------------
# set rt [ regexp  {^([0-9]+[a-z]+|[A-Z]+[0-9]+)$}  123abc ] 
# puts $rt 

puts --------------------

set bid 2
set ChipNo 5

# ---------------------------------------------------------
# AU时钟芯片配置文件通过wetest直接配置时钟芯片
# @param1:板ID
# @param2:ChipNo ,使用cfg-get-chipinfo 可获得取值
# @param3:au时钟配置文件路径
# ---------------------------------------------------------
proc write_time_chip_au { bid ChipNo filepath } {
    set fw [open temp.tcl w+]; # 获得写文件句柄 这种方法将会覆盖文件原有内容
    # puts $fw "I am writing context to new.txt" ;#向文件new.txt写入内容

    set fp [open $filepath r] 
    while {![eof $fp] } {
        gets $fp line
        # puts $line
        set line [string trim $line]
        # i2c.write( , , ) 匹配
        if { [regexp  {(^i2c.write\()(0x[0-9abcdef]+)(,)(0x[0-9abcdef]+)(,)(0x[0-9abcdef]+)}  "$line"  str head dev dh1 addr dh2 data] } {
            # puts "cfg-set-chipreg:$bid,$ChipNo,$addr,$data"
            puts $fw "puts cfg-set-chipreg:$bid,$ChipNo,$addr,$data"
        } elseif { [regexp {(^time.sleep)} $line] } { 
            puts $fw "puts \"sleep 0.1\""
        }
    }
    close $fp 
    close $fw

    # 执行生成的子脚本
    set fp [open temp.tcl r];#读模式获取 temp.tcl文件的句柄
    set cmd [read $fp];#读取my.tcl文件的内容
    eval $cmd  ;#执行读取文件中的tcl指令
    close $fp ;#关闭文件句柄
}

write_time_chip_au 6 5 au.py



